package com.loan.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.utils.StringUtils;
import com.loan.system.domain.CompanyEntity;
import com.loan.system.domain.PayBankEntity;
import com.loan.system.mapper.PayBankMapper;
import com.loan.system.service.PayBankService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author shiguang
 * @date 2022年09月15日 21:59
 */
@Service
@RequiredArgsConstructor
public class PayBankServiceImpl implements PayBankService {

    private final PayBankMapper payBankMapper;

    @Override
    public TableDataInfo<PayBankEntity> selectPageList(PayBankEntity entity, PageQuery pageQuery) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<PayBankEntity> lqw = new LambdaQueryWrapper<PayBankEntity>()
                .like(StringUtils.isNotBlank(entity.getBankName()), PayBankEntity::getBankName, entity.getBankName())
                .orderByDesc(PayBankEntity::getCreateTime);
        Page<PayBankEntity> page = payBankMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public PayBankEntity selectPayBankById(Long id) {
        return payBankMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addPayBank(PayBankEntity entity) {
        //查询是否有上架的支付银行卡
         long count = payBankMapper.selectCount(new QueryWrapper<PayBankEntity>().eq("del_flag", "0").eq("status", "0"));
         if (count > 0){
             entity.setStatus("1");
         }
        return payBankMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePayBank(PayBankEntity entity) {
        return payBankMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePayBankById(Long id) {
        return payBankMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStatus(PayBankEntity entity) {
        //查询是否有上架的支付银行卡
//        if (entity.getStatus().equals("0")) {
//            PayBankEntity payBankEntity = payBankMapper.selectOne(new QueryWrapper<PayBankEntity>().eq("del_flag", "0").eq("status", "0"));
//            if (ObjectUtil.isNotNull(payBankEntity)) {
//                payBankEntity.setStatus("1");
//                payBankMapper.updateById(payBankEntity);
//            }
//        }
        return payBankMapper.updateById(entity);
    }

    @Override
    public List<PayBankEntity> getList() {
        return payBankMapper.selectList(new QueryWrapper<PayBankEntity>().eq("status","0").eq("del_flag","0"));
    }


}
